package com.kstudio.ksnapshot.klist;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.kstudio.ksnapshot.KUtil;
import com.kstudio.ksnapshot.R;
import com.kstudio.ksnapshot.view.KSubIconView;

public class KListAdapter extends ArrayAdapter<KListFileData> {
    KUtil log = new KUtil("KListAdapter");

    private static final int ICON_FOLDER        = R.drawable.klist_folder;
    private static final int ICON_FILE          = R.drawable.klist_file;
    private static final int ICON_EXTRA_VIDEO   = R.drawable.klist_video_icon;
    private static final int ICON_EXTRA_PICTURE = R.drawable.klist_picture_icon;

    private final LayoutInflater kInflater;

    public KListAdapter(Context context, ArrayList<KListFileData> objects) {
        super(context, 0, objects);

        kInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        KListViewWrapper viewWrapper;

        if(view == null) {
            view = kInflater.inflate(R.layout.klist_row, null);
            viewWrapper = new KListViewWrapper(view);
            view.setTag(viewWrapper);
        } else {
            viewWrapper = (KListViewWrapper)view.getTag();
        }

        final KListFileData data = this.getItem(position);
        if(data != null) {
            if(data.getAttribute() == KListFileData.FOLDER) {
                viewWrapper.getSubIcon().setMainIconId(ICON_FOLDER);
                viewWrapper.getSubIcon().setSubIconId(KSubIconView.SubIconNone);
                viewWrapper.getName().setText(data.getName());
                viewWrapper.getExecution().setVisibility(View.GONE);
            } else if(data.getAttribute() == KListFileData.FILE) {
                viewWrapper.getSubIcon().setMainIconId(ICON_FILE);
                viewWrapper.getSubIcon().setSubIconId(KSubIconView.SubIconNone);
                viewWrapper.getName().setText(data.getName());
                viewWrapper.getExecution().setVisibility(View.GONE);
            } else if(data.getAttribute() == KListFileData.VIDEO) {
                if(data.getBitmap() == null) {
                    viewWrapper.getSubIcon().setMainIconId(ICON_FILE);
                } else {
                    viewWrapper.getSubIcon().setMainIconBMP(data.getBitmap());
                }
                viewWrapper.getSubIcon().setSubIconId(ICON_EXTRA_VIDEO);
                viewWrapper.getName().setText(data.getName());
                viewWrapper.getExecution().setVisibility(View.VISIBLE);
                viewWrapper.getDuration().setText(formatTime(data.getDuration()));
            } else if(data.getAttribute() == KListFileData.PICTURE) {
                if(data.getBitmap() == null) {
                    viewWrapper.getSubIcon().setMainIconId(ICON_FILE);
                } else {
                    viewWrapper.getSubIcon().setMainIconBMP(data.getBitmap());
                }
                viewWrapper.getSubIcon().setSubIconId(ICON_EXTRA_PICTURE);
                viewWrapper.getName().setText(data.getName());
                viewWrapper.getExecution().setVisibility(View.GONE);
            }
        }

        return view;
    }

    private String formatTime(int duration) {
        int hour = (duration/1000) / 3600;
        int min = ((duration/1000) % 3600) / 60;
        int sec = (duration/1000) % 60;

        if(hour == 0) {
            return String.format("%02d:%02d", min, sec);
        } else {
            return String.format("%02d:%02d:%02d", hour, min, sec);
        }
    }
}
